home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / PDUVariable.java < prev    next >
Text File  |  1997-06-09  |  2KB  |  107 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. /**
  18.  * This class implements a pair of SNMP Object ID and Type.  It
  19.  * is used in PDU which stores a list these pairs
  20.  * @see PDU
  21.  * @version     $Id: PDUVariable.java,v 1.4 1997/05/18 08:11:54 alex Exp $
  22.  * @author      Alex Kowalenko
  23.  */
  24.  
  25. public class PDUVariable implements BERSerializable {
  26.   
  27.   private ObjectId  _id;
  28.   private Type _value;
  29.   
  30. /**
  31.  * Constructor
  32.  */
  33.  
  34.     PDUVariable(ObjectId id, Type value) {
  35.     _id = id;
  36.     _value = value;
  37.     };
  38.   
  39. /**
  40.  * Create a pair from the chacter sequence in buf.  When
  41.  * finished the character that consituted the the pair will
  42.  * be removed from buf.
  43.  */
  44.  
  45.     PDUVariable(ByteBuffer buf) throws SnmpPDUException {
  46.     // Get CONSTRUCTOR | SEQUENCE
  47.     if(buf.byteAt(0) != (ASN.SEQUENCE | ASN.CONSTRUCTOR)) {
  48.         throw new SnmpPDUException("PDUVariable: Not a sequence of Variable-Value");
  49.     };
  50.     buf.removeBeginning(1);
  51.     
  52.     int length = ASN.getLength(buf);
  53.     
  54.     // Parse Object ID
  55.     if(buf.byteAt(0) != ASN.OBJECT_ID) { 
  56.         throw new SnmpPDUException("PDUVariable: Expecting an Object ID");
  57.     }
  58.     buf.removeBeginning(1);
  59.     _id = new ObjectId(buf);
  60.     
  61.     // Parse Value
  62.     _value = Type.parse(buf);
  63.     return;
  64.     };
  65.     
  66. /**
  67.  * Return the SNMP Object ID from the pair.
  68.  */
  69.  
  70.     ObjectId oid() {
  71.     return _id;
  72.     };
  73.  
  74. /**
  75.  * Return the Type object from the pair.
  76.  */
  77.  
  78.     Type value() {
  79.     return _value;
  80.     };
  81.  
  82. /**
  83.  * Create a sequence of characters that represent the pair.
  84.  * according to the rules of SNMP Protocol.
  85.  */
  86.  
  87.     public ByteBuffer BERSerialize() {
  88.     ByteBuffer buffer = new ByteBuffer();
  89.     buffer.append(_id.BERSerialize());
  90.     buffer.append(_value.BERSerialize());
  91.     ByteBuffer newbuffer = new ByteBuffer();
  92.     newbuffer.append((byte) (ASN.SEQUENCE | ASN.CONSTRUCTOR ));
  93.     newbuffer.append(ASN.buildLength(buffer.size())); 
  94.     newbuffer.append(buffer);
  95.     return newbuffer;
  96.     };
  97.  
  98. /**
  99.  * Creates a String representation 
  100.  */
  101.  
  102.   public String toString() {
  103.       return "{" + _id + ", " + _value + "}";
  104.   }
  105.   
  106. };
  107.